package com.dhecp.project.common.controller;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dhecp.common.core.controller.BaseController;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.project.app.service.ModuleDataService;
import com.dhecp.project.flowable.service.ActProcessTaskService;
import com.dhecp.project.system.service.SysDictionaryMobileForFieldService;

/**
 * 通用业务数据接口 控制层
 * @author DH-Xxj
 * @version 2020年9月1日
 */
@Controller
@RequestMapping("/common/moduledata")
public class ModuleDataController extends BaseController{
	
	@Autowired
    private ModuleDataService moduleDataService;

	@Autowired
    private SysDictionaryMobileForFieldService dictionaryMobileForFieldService;
    
    @Autowired
    private ActProcessTaskService actProcessTaskService;
	
	/**
	 * 返回单据精简审核页面数据
	 * @param nid 必要参数：单据nid
	 * @param moduleTable 必要参数：单据功能唯一标识
	 * @return
	 * @author DH-Xxj
	 */
    @SuppressWarnings("unchecked")
	@GetMapping("/audit")
    @ResponseBody
    public AjaxResult auditPage(@RequestParam Map<String, Object> params){
    	Map<String, Object> pageDate = new LinkedHashMap<String, Object>();
    	Map<String, Object> fieldContentMap = new HashMap<>();
    	if (params.get("nid") == null || params.get("nid") == ""){
    		return AjaxResult.error("缺少必要参数：单据编码[nid]");
    	}
    	if (params.get("moduleTable") == null || params.get("moduleTable") == ""){
    		return AjaxResult.error("缺少必要参数：单据功能唯一标识[moduleTable]");
    	}
    	String nid = params.get("nid").toString();
        String activeFlow = "flowzd";
        String activeFlowName = "准备制单";
    	try{
        	pageDate = dictionaryMobileForFieldService.queryMobileFieldAttributesMap(params);
            Map<String, Object> data = moduleDataService.queryModulePrimaryDataByNid(params);
            if( data == null ){
				return AjaxResult.error("未找到该任务！");
			}
            for (String key : data.keySet()){
            	fieldContentMap.put(key.toLowerCase(), data.get(key));
            	if ("activeflow".equals(key.toLowerCase())) activeFlow = data.get(key).toString();
            }
            
            //查询流程状态中文说明
            if (!"".equals(nid) && !"0".equals(nid)){
            	try{
                    activeFlowName = actProcessTaskService.queryActivitiImplNameByNid(nid);
            	}catch(Exception e){
            		activeFlowName = "未知环节";
            	}
            }
            if ("".equals(activeFlowName) || activeFlowName == null){
            	if ("end".equals(activeFlow)){
            		activeFlowName = "结束";
            	}else{
                	activeFlowName = "未知环节";
            	}
            }else if ("未知环节".equals(activeFlowName) && "end".equals(activeFlow)){
        		activeFlowName = "结束";
        	}
            
            for (String key : pageDate.keySet()){
            	Map<String, Object> fieldMap = new HashMap<>();
            	fieldMap = (Map<String, Object>) pageDate.get(key);
            	if ("activeflowname".equals(key)){
                	fieldMap.put("fieldContent", activeFlowName);
            	}else{
                	fieldMap.put("fieldContent", fieldContentMap.get(key));
            	}
            }
    	} catch (Exception e) {
//    		return AjaxResult.error("功能唯一标识无效或当前单据暂未开放手机审核页面。");   
    		return AjaxResult.error(this.getExceptionMessage(e).toString());       
        }

        return AjaxResult.success(pageDate);
	}
	
}
